#!/usr/bin/perl

use strict;

use API::Document;
use API::Index;
use API::Statistics;

# read the path to cranfield corpus from command line
my $cranfield_dir = $ARGV[0];
if (!defined $cranfield_dir) {
  die "Usage:  ./index /path/to/the/corpus\n";
}

my $dest_dir = "txt_corpus";

# create a directory to store plain text corpus
if (-d $dest_dir) { 
  print "dir \"$dest_dir\" exists, overwrite it!\n";
  `rm -rf $dest_dir`;
}
system("mkdir $dest_dir");

my $meta_hashs = Index::create_meta_dbm();

# process each XML file. Extract metadata and convert to plain text 
print "converting XML files in \"$cranfield_dir\" to generic txt format\n";
my @docs=<$cranfield_dir/*>;
foreach my $doc(@docs) {
  my $hash = Document::parse_XML $doc;
  Document::convert_to_generic $hash, $dest_dir;
  Index::store_metadata $hash, $meta_hashs;
}

Index::close_meta_dbm();

# build the index
my $istart = time;
# creating index for the corpus
Index::create_index($dest_dir);
my $itime = time;
print "Done!\n\n";
$itime -= $istart;

my @stat = Statistics::stat_of_index();

# display basic statistics
print "###################################\n";
printf "# time elapsed:       %3d seconds #\n", $itime;
printf "# number of words (stemmed): %d #\n", $stat[0];
printf "# number of docs:      %-10d #\n", $stat[1];
printf "# size of index:       %-7d KB #\n", $stat[2];
print "###################################\n";

